package com.jeeplus.common.json;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;

/**
 * 采用高版本的Jackson，包名com.fasterxml.jackson 支持JsonGetter、JsonSetter注解
 * 调用时注意Jackson版本
 * 
 * @author Elvin Zhao - 279152260@qq.com
 * @version 1.0 Created on 2018年1月29日 上午10:43:25
 */
public final class JsonTransformer2 {

	private final static ObjectMapper OBJ_MAPPER = new ObjectMapper();

	private JsonTransformer2() {
	}

	public static <T> T json2Object(String value, Class<T> clazz) {

		if (StringUtils.isBlank(value)) {
			return null;
		}

		T t = null;
		try {
			t = OBJ_MAPPER.readValue(value, clazz);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {

		}

		return t;
	}

	public static <T> String object2Json(T object) {

		if (object == null) {
			return null;
		}

		try {
			return OBJ_MAPPER.writeValueAsString(object);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {

		} catch (IOException e) {

		}

		return null;
	}

	public static <T> String object2JsonByFilter(T object, FilterProvider filter) {

		if (object == null) {
			return null;
		}

		try {

			return OBJ_MAPPER.setFilterProvider(filter).writeValueAsString(object);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {

		} catch (IOException e) {

		}

		return null;
	}
}
